package de.hmm.hwip.connection.client;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

import android.content.SharedPreferences;
import android.util.Log;
import de.hmm.hwip.connection.api.IConIn;
import de.hmm.hwip.general.IAndroidContext;

/**
 * OSGi network service
 * Listen on Socket, wait for connection
 * 
 * @author johannes mendel
 * @version 27.06.2012
 *
 */
public class Activator implements BundleActivator{

	private final String TAG = "Connection Client (GW)";
	/**
	 * service Tracker to the network service
	 */
	private ServiceTracker serviceTracker;
	
	/**
	 * network osgi service Interface
	 */
	private static IConIn service = null;
	
	/**
	 * Android Context service tracker
	 */
	private ServiceTracker contextServiceTracker;
	
	/**
	 * service for Android App Context
	 */
	private IAndroidContext androidContextService;
	/**
	 * receives commands from gui
	 *
	private IRequest guiservice;*/

	/**
	 * OSGi Bundle Context
	 */
	private static BundleContext context;
	

	/**
	 * start the service
	 */
	
	@Override
	public void start(BundleContext context) throws Exception {
		Log.i(TAG,"=========== de.hmm.hwip.connection.client =========== started ===========");

		// 		getting the network service
		serviceTracker = new ServiceTracker(context, IConIn.class.getName(), null );
		serviceTracker.open();
    	service = (IConIn) serviceTracker.getService();
    	
    	// 		getting the android context service
        contextServiceTracker = new ServiceTracker(context, IAndroidContext.class.getName(), null );
        contextServiceTracker.open();
  
        
        synchronized(this){
        	while(true){
        	    androidContextService = (IAndroidContext) contextServiceTracker.getService();
        		if(androidContextService != null){
                	SharedPreferences settings = androidContextService.transferContext().getSharedPreferences("hwip", 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putBoolean("isgateway", true);
                    editor.commit();
                    break;
                }
        		Log.w(TAG, "Android Context is not available ... wait 5 sec.");
        		wait(5000);
        	}
        }
        
		//guiservice = new GuiCommandReceiver();
		/*
        Hashtable<String, Object> params = new Hashtable<String, Object>();
        params.put( org.osgi.framework.Constants.SERVICE_PID, IRequest.class.getName());
        params.put( org.osgi.framework.Constants.SERVICE_DESCRIPTION,"Provides Gui Service");
        context.registerService(IRequest.class.getName(), guiservice, params);
    	*/
        Activator.context = context;
        
    	if (service != null)
    	{
    		openGatewaySocket(context);
    		openSpotSocket(context);	
    	}
		
		
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		closeSocket();
		Log.i(TAG, "Socket listener service stoped ...");
	}
	
	public static BundleContext getContext()
	{
		return context;
	}
	
	/**opens Spot socket*/
	private void openSpotSocket(BundleContext context)
	{
		SpotServer spotServer = new SpotServer(service, context);
		Thread ts = new Thread(spotServer);
		ts.start();
	}
	
	/** opens Gateway socket **/
	private void openGatewaySocket(BundleContext context)
	{
		GatewayServer gateServer = new GatewayServer(service, context);
		Thread tg = new Thread(gateServer);
		tg.start();
	}
	
	/** closes socket **/
	private void closeSocket()
	{
		SpotServer.closeSpot();
		GatewayServer.closeGateway();
	}
	
	public static void getRest(){
	//	service.getRest();
	}
}
